home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT43.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  9.4 KB  |  314 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                 Demonstration Program 43                          
  5.                                           
  6.  VESA HICOLOR demonstration file. Any program which uses the WGT VESA library        
  7.  requires the existence of a VESA driver. Most video card manufacturers      
  8.  include software drivers with their installation diskettes. You can also    
  9.  find drivers for most popular cards on a BBS.                               
  10.                                           
  11.  This program demonstrates how to access and use video modes in VESA with
  12.  more than 256 colors. In order to do this, the card must have enough memory
  13.  to support the video modes since each pixel on the screen requires several
  14.  bytes.
  15.                                           
  16.  *** PROJECT ***                                                             
  17.  This program requires the WGT5_WC.LIB and WVESA_WC.LIB files to be linked.  
  18.                                           
  19.  *** DATA FILES ***                                                          
  20.  None                                                           
  21.                                WATCOM C++ VERSION 
  22. ==============================================================================
  23. */
  24.  
  25. #include <graph.h>
  26. #include <wgt5.h>
  27. #include <wgtvesa.h>
  28.  
  29. short videomode[24];    /* Array to hold supported modes */
  30.  
  31. short modenums[24] = { 0x10d, 0x11d, 0x11e, 0x110, 0x113, 0x116,
  32.                0x119, 0x125, 0x10e, 0x11f, 0x120, 0x111,
  33.                0x114, 0x117, 0x11a, 0x126, 0x10f, 0x121,
  34.                0x122, 0x112, 0x115, 0x118, 0x11b, 0x127 };
  35.  
  36. int totalmodes;         /* Total number of supported VESA modes */
  37.  
  38. char vstring[24][18] = { "320  x 200  x 32k", "640  x 350  x 32k",
  39.              "640  x 400  x 32k", "640  x 480  x 32k",
  40.              "800  x 600  x 32k", "1024 x 768  x 32k",
  41.              "1280 x 1024 x 32k", "1600 x 1200 x 32k",
  42.              "320  x 200  x 64k", "640  x 350  x 64k",
  43.              "640  x 400  x 64k", "640  x 480  x 64k",
  44.              "800  x 600  x 64k", "1024 x 768  x 64k",
  45.              "1280 x 1024 x 64k", "1600 x 1200 x 64k",
  46.              "320  x 200  x 16m", "640  x 350  x 16m",
  47.              "640  x 400  x 16m", "640  x 480  x 16m",
  48.              "800  x 600  x 16m", "1024 x 768  x 16m",
  49.              "1280 x 1024 x 16m", "1600 x 1200 x 16m" };
  50.  
  51. short bytesperpixel;    /* Used to keep track of the # of bytes per pixel */
  52.  
  53.  
  54. void getmodes (void)
  55. {
  56.   short counter;
  57.  
  58.   totalmodes = 0;               /* Start counter at 0 modes supported */
  59.  
  60.   /* Now find supported modes and add them to our array */
  61.   for (counter = 0; counter < 24; counter++)
  62.   {
  63.     if (wvesa_supported (modenums[counter]))
  64.     {
  65.       videomode[totalmodes] = modenums[counter];
  66.       totalmodes++;
  67.     }
  68.   }
  69. }
  70.  
  71.  
  72. /* This function simply returns a string number to display based on the
  73.    highlighted video mode */
  74. short which_string (int mode)
  75. {
  76.   short counter;
  77.  
  78.   for (counter = 0; counter < 24; counter++)
  79.     if (modenums[counter] == mode)
  80.       return counter;
  81.   return 0;
  82. }
  83.  
  84.  
  85. short select_mode (void)
  86. {
  87.   short ctr;
  88.   short done;
  89.   short selected;
  90.   struct rccoord endy;
  91.   char ch;
  92.  
  93.   printf ("\nPress ENTER to selected highlighted mode, any other key advances highlight.\n");
  94.  
  95.   for (ctr = 0; ctr < totalmodes; ctr++)        /* Show supported modes */
  96.   {
  97.     _settextposition (12 + ctr, 1);
  98.     _outtext (vstring[ which_string(videomode[ctr]) ]);
  99.   }
  100.   endy = _gettextposition ();
  101.  
  102.   selected = 0;
  103.   done = 0;
  104.   while (!done)
  105.   {
  106.     _settextcolor (12);                       /* Highlight string */
  107.     _settextposition (12 + selected, 1);
  108.     _outtext (vstring[ which_string(videomode[selected]) ]);
  109.     ch = getch ();
  110.     if (ch == 13)                               /* Abort when ENTER pressed */
  111.       done = 1;
  112.     else {
  113.       while (kbhit ()) getch ();  
  114.       _settextcolor (7);                    /* De-highlight previous */
  115.       _settextposition (12 + selected, 1);
  116.       _outtext (vstring[ which_string(videomode[selected]) ]);
  117.       selected++;
  118.       if (selected >= totalmodes)               /* Wrap around list */
  119.     selected = 0;
  120.     }
  121.   }
  122.   _settextposition (endy.row + 1, 1);
  123.  
  124.   return videomode[selected];           /* Return the selected mode */
  125. }
  126.  
  127.  
  128. void hi_pixel (short x, short y, unsigned int color)
  129. {
  130.   int bankpos;
  131.   block dst;
  132.  
  133.   /* Now find the offset in video ram we want to set */
  134.   bankpos = (y * VESAmode.BytesPerScanLine) + (x * bytesperpixel);
  135.  
  136.   /* Now switch to the appropriate bank of memory in the video card */
  137.   wvesa_bank (bankpos / VESAbanksize);
  138.  
  139.   /* Set our pointer to the visual screen (abuf) plus the offset within the
  140.      bank that we want to set */
  141.   dst = abuf + (bankpos % VESAbanksize);
  142.  
  143.   /* Now set it */
  144.   memcpy (dst, &color, bytesperpixel);
  145. }
  146.  
  147.  
  148. void do_pixels (void)
  149. {
  150.   short x, y;
  151.   unsigned int ctr;
  152.   unsigned int lastcolor;
  153.  
  154.   ctr = 0;
  155.   lastcolor = 1 << VESAmode.BitsPerPixel;
  156.   for (x = 0; x < VESAmode.XResolution; x++)
  157.     for (y = 0; y < VESAmode.YResolution; y++)
  158.     {
  159.       hi_pixel (x, y, ctr);
  160.       ctr++;
  161.       if (ctr > lastcolor)
  162.     ctr = 0;
  163.     }
  164.   getch();
  165. }
  166.  
  167.  
  168. void load_tga(char *filename)
  169. {
  170.   FILE *in;
  171.   struct {
  172.     unsigned char  identsize;
  173.     unsigned char  colormaptype;
  174.     unsigned char  imagetype;
  175.     unsigned short colormapstart;
  176.     unsigned short colormaplength;
  177.     unsigned char  colormapbits;
  178.     unsigned short xstart;
  179.     unsigned short ystart;
  180.     unsigned short width;
  181.     unsigned short depth;
  182.     unsigned char  bits;
  183.     unsigned char  descriptor;
  184.   } TGA_Header;
  185.   int size, ctr, bankpos;  
  186.   unsigned int color;
  187.   unsigned char temp;
  188.   unsigned char *targa, *dst;
  189.   int i;
  190.  
  191.   in = fopen (filename, "rb");
  192.   fread (&TGA_Header, sizeof (TGA_Header), 1, in);  
  193.   if (TGA_Header.imagetype == 2)
  194.   {
  195.     for (i = 1; i <= TGA_Header.identsize; i++)
  196.       fread (&temp, 1, 1, in);
  197.     size = TGA_Header.width * TGA_Header.depth * 3;
  198.     targa = (unsigned char *)malloc (size);
  199.     for (i = 0; i < size; i+= 3)
  200.     {
  201.       targa[i] = fgetc (in);
  202.       targa[i + 1] = fgetc (in);
  203.       targa[i + 2] = fgetc (in);
  204.     }
  205.     for (i = TGA_Header.depth - 1; i >= 0; i--)
  206.       for (ctr = 0; ctr < TGA_Header.width; ctr++)
  207.       {
  208.     bankpos = (i * VESAmode.BytesPerScanLine) + (ctr * bytesperpixel);
  209.     wvesa_bank (bankpos / VESAbanksize);
  210.     dst = abuf + (bankpos % VESAbanksize);
  211.     color = 0;
  212.     memcpy (&color, targa, 3);
  213.     memcpy (dst, &color, bytesperpixel);
  214.     targa += 3;
  215.       }
  216.   }
  217.   fclose (in);
  218. }
  219.  
  220.  
  221. void main(void)
  222. {
  223.   int oldmode;                  /* Video mode before program was started */
  224.   int mymode = 0;               /* Selected video mode */
  225.  
  226.   printf ("WGT Example #43\n\n");
  227.   printf ("VESA SVGA and HICOLOR modes are demonstrated providing a driver is present.\n");
  228.   printf ("Pixels are drawn in 640x480 x 16 million color mode.\n"); 
  229.   printf ("\n\nPress any key to continue.\n");
  230.   getch ();
  231.  
  232.   oldmode = wgetmode ();         /* Preserve our original video mode */
  233.   _clearscreen ( _GCLEARSCREEN); /* Clear the screen */
  234.   if (wvesa_detected ())         /* Look for VESA driver */
  235.     printf ("SVGA detected.\n");
  236.   else
  237.   {
  238.     printf ("SVGA support not found. Please check for VESA driver presence.\n");
  239.     exit (1);
  240.   }
  241.  
  242.   /* Display the video card maunfacturer's string */
  243.   printf ("VESA version %x\n", VGA.VESAVersion);
  244.   printf ("VIDEO CARD OEM STRING:\n%s\n", VGA.OEMStringPtr);
  245.   if (VGA.VESAVersion >= 0x200)
  246.   {
  247.     printf ("OEM Software revision #%x\n", VGA.OemSoftwareRev);
  248.     printf ("OEM Vendor Name: %s\n", VGA.OemVendorNamePtr);
  249.     printf ("OEM Product Name: %s\n", VGA.OemProductNamePtr);
  250.     printf ("OEM Product Revision: %s\n", VGA.OemProductRevPtr);
  251.   }
  252.     else printf ("\n\n");
  253.  
  254.   printf ("Card contains %dk of video RAM.\n", VGA.TotalMemory * 64);
  255.  
  256.   getmodes ();                           /* Find supported video modes */
  257.  
  258.   if (totalmodes == 0)
  259.   {
  260.     printf ("Hicolor SVGA modes not supported. Program aborted.\n");
  261.     exit (1);
  262.   }
  263.   else
  264.   {
  265.     mymode = select_mode ();
  266.   }    
  267.  
  268.   if (!wvesa_getmodeinfo (mymode))
  269.     printf ("Mode detection failed.\n");
  270.   else
  271.   {
  272.     printf ("\nMode %x selected.\n\n", mymode);
  273.     printf ("X resolution : %5d\nY resolution : %5d\n", VESAmode.XResolution, VESAmode.YResolution);
  274.     printf ("Banks: %d\n", VESAmode.NumberOfBanks);
  275.     printf ("Window Granularity : %d\n", VESAmode.WinGranularity);
  276.     printf ("Window size in Kb  : %d\n", VESAmode.WinSize);
  277.     if (VESAmode.WinAAttributes & 1 == 0)
  278.       printf ("Window A not supported\n");
  279.     else
  280.       printf ("Window A Segment   : %x\n", VESAmode.WinASegment);
  281.     if (VESAmode.WinBAttributes & 1 == 0)
  282.       printf ("Window B not supported\n");
  283.     else
  284.       printf ("Window B Segment   : %x\n", VESAmode.WinBSegment);
  285.     printf ("Bits per pixel: %d\n", VESAmode.BitsPerPixel);
  286.     printf ("Bytes per scanline: %d\n", VESAmode.BytesPerScanLine);
  287.   }
  288.  
  289.   printf ("\n\nPRESS ANY KEY TO ENTER GRAPHICS MODE\n");
  290.   getch ();
  291.  
  292.   vga256 ();
  293.   if (!wvesa_init (mymode))
  294.   {
  295.     printf ("Unable to initialize graphics mode.\n");
  296.     exit (1);
  297.   }
  298.  
  299.   bytesperpixel = (VESAmode.BitsPerPixel + 1) / 8;
  300.   do_pixels ();                         /* Perform our test */
  301.   
  302.   /* Attempt to use 640x480x16m */
  303.   /*
  304.   if (wvesa_supported (0x112))
  305.   {
  306.     wvesa_init (0x112);
  307.     bytesperpixel = (VESAmode.BitsPerPixel + 1) / 8;
  308.     load_tga ("hicolor.tga");
  309.   } 
  310.   getch ();*/
  311.   wsetmode (oldmode);                    /* Return text mode */
  312. }
  313.  
  314.